package com.oa.ylms.apps.dao.assess.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oa.ylms.apps.constants.assess.IAssessInfoStates;
import com.oa.ylms.apps.dao.assess.IAssessInfoDao;
import com.oa.ylms.apps.entity.assess.AssessInfoValue;
import com.oa.ylms.apps.entity.assess.AssessModelValue;

@SuppressWarnings("unchecked")
public class AssessInfoDaoImpl extends HibernateDaoSupport implements
		IAssessInfoDao {

	private List<AssessInfoValue> list;
	private int numbers;

	public int addAssessInfo(AssessInfoValue assessInfo) throws Exception {
		if (assessInfo != null) {
			this.getHibernateTemplate().save(assessInfo);

		}
		return assessInfo.getAssessId();
	}

	public boolean editAssessInfo(AssessInfoValue assessInfo) throws Exception {
		if (assessInfo != null) {
			try {
				this.getHibernateTemplate().update(assessInfo);

			} catch (Exception e) {
				return false;
			}
			return true;
		}
		return false;
	}

	public int countTpages(final int states, final int yearNo,
			final int monthNo, final AssessModelValue assessModel,
			final AssessInfoValue assessInfo, int rows) {
		int pageNumbers = 0;
		String hqlStr = "SELECT aiv FROM AssessInfoValue aiv, AssessModelValue amv where aiv.assessModel.modelId = amv.modelId";
		if (yearNo != IAssessInfoStates.UNDEFINED) {
			hqlStr += " AND aiv.yearNo = ?";
		}
		if (monthNo != IAssessInfoStates.UNDEFINED) {
			hqlStr += " AND aiv.monthNo = ?";
		}
		if (assessModel.getModelName() != null) {
			hqlStr += " AND amv.modelName= ? ";
		}
		if (assessInfo.getAssessName() != null) {
			hqlStr += " AND aiv.assessName = ?";
		}
		if (states == IAssessInfoStates.SAVED) {
			hqlStr += " AND aiv.states = ?";
		}
		if (states == IAssessInfoStates.PASSED) {
			hqlStr += " AND aiv.states = ? ";
		}
		if (states == IAssessInfoStates.CHECKING) {
			hqlStr += " AND aiv.states <> ? AND aiv.states <> ? AND aiv.states <>?";
		}
		final String hql = hqlStr;
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = -1;
				if (yearNo != IAssessInfoStates.UNDEFINED) {
					index++;
					query.setInteger(index, yearNo);
				}
				if (monthNo != IAssessInfoStates.UNDEFINED) {
					index++;
					query.setInteger(index, monthNo);
				}
				if (assessModel.getModelName() != null) {
					index++;
					query.setString(index, assessModel.getModelName());
				}
				if (assessInfo.getAssessName() != null) {
					index++;
					query.setString(index, assessInfo.getAssessName());
				}
				if (states == IAssessInfoStates.SAVED) {
					index++;
					query.setInteger(index, IAssessInfoStates.SAVED);
				}
				if (states == IAssessInfoStates.PASSED) {
					index++;
					query.setInteger(index, IAssessInfoStates.PASSED);
				}
				if (states == IAssessInfoStates.CHECKING) {
					index++;
					query.setInteger(index, IAssessInfoStates.PASSED);
					index++;
					query.setInteger(index, IAssessInfoStates.FAILED);
					index++;
					query.setInteger(index, IAssessInfoStates.SAVED);
				}
				list = query.list();
				return list;
			}
		});
		this.numbers = list.size();
		if (numbers <= rows) {
			pageNumbers = 1;
		}
		if (numbers == 0) {
			pageNumbers = 0;
		} else {
			if (numbers % rows == 0) {
				pageNumbers = numbers / rows;
			} else {
				pageNumbers = numbers / rows + 1;
			}
		}
		return pageNumbers;
	}

	public List<AssessInfoValue> queryAssessInfoWithPageByStates(
			final int states, final int yearNo, final int monthNo,
			final AssessModelValue assessModel,
			final AssessInfoValue assessInfo, final int rows, final int pageNo) {
		String hqlStr = "SELECT aiv FROM AssessInfoValue aiv, AssessModelValue amv where aiv.assessModel.modelId = amv.modelId ";
		if (yearNo != IAssessInfoStates.UNDEFINED) {
			hqlStr += " AND aiv.yearNo = ?";
		}
		if (monthNo != IAssessInfoStates.UNDEFINED) {
			hqlStr += " AND aiv.monthNo = ?";
		}
		if (assessModel.getModelName() != null) {
			hqlStr += " AND amv.modelName= ? ";
		}
		if (assessInfo.getAssessName() != null) {
			hqlStr += " AND aiv.assessName = ?";
		}
		if (states == IAssessInfoStates.SAVED) {
			hqlStr += " AND aiv.states = ?";
		}
		if (states == IAssessInfoStates.PASSED) {
			hqlStr += " AND aiv.states = ?";
		}
		if (states == IAssessInfoStates.CHECKING) {
			hqlStr += " AND aiv.states <> ? AND aiv.states <> ? AND aiv.states <>?";
		}
		final String hql = hqlStr;
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = -1;
				if (yearNo != IAssessInfoStates.UNDEFINED) {
					index++;
					query.setInteger(index, yearNo);
				}
				if (monthNo != IAssessInfoStates.UNDEFINED) {
					index++;
					query.setInteger(index, monthNo);
				}
				if (assessModel.getModelName() != null) {
					index++;
					query.setString(index, assessModel.getModelName());
				}
				if (assessInfo.getAssessName() != null) {
					index++;
					query.setString(index, assessInfo.getAssessName());
				}
				if (states == IAssessInfoStates.SAVED) {
					index++;
					query.setInteger(index, IAssessInfoStates.SAVED);
				}
				if (states == IAssessInfoStates.PASSED) {
					index++;
					query.setInteger(index, IAssessInfoStates.PASSED);
				}
				if (states == IAssessInfoStates.CHECKING) {
					index++;
					query.setInteger(index, IAssessInfoStates.PASSED);
					index++;
					query.setInteger(index, IAssessInfoStates.FAILED);
					index++;
					query.setInteger(index, IAssessInfoStates.SAVED);
				}
				query.setFirstResult(rows * (pageNo - 1));
				query.setMaxResults(rows);
				list = query.list();
				return list;
			}
		});
		return list;
	}

	public AssessInfoValue queryAssessInfoByAssessId(int assessId)
			throws Exception {

		return (AssessInfoValue) this.getHibernateTemplate().get(
				AssessInfoValue.class, assessId);
	}

	public boolean modifyAssessInfo(AssessInfoValue assessInfo)
			throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

	public List<AssessInfoValue> getList() {
		return list;
	}

	public void setList(List<AssessInfoValue> list) {
		this.list = list;
	}

	public int getNumbers() {
		return numbers;
	}

	public void setNumbers(int numbers) {
		this.numbers = numbers;
	}

	public int getTotalNumber() throws Exception {
		return numbers;
	}

}
